package com.forenow.demo.newdemo7;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeeDAOImpl implements EmployeeDAO {
    List<Employee> list1=new ArrayList();;
    Scanner input= new Scanner(System.in);
    @Override
    public void add(Employee employee) {
                list1.add(employee);
                return ;

    }

    @Override
    public void delete(Employee employee) {
        for (int i = 0; i <list1.size(); i++) {
            if (employee == list1.get(i)) {
                list1.remove(i);
            }
        }
        for (int i = 0; i <list1.size(); i++) {
            System.out.println(list1.get(i).getId()+" "+list1.get(i).getName()+" "+list1.get(i).getSal()+" "+list1.get(i).getDept());
        }

    }

    @Override
    public void updata(Employee employee) {
        System.out.println("输入员工id");
        int id=input.nextInt();
        System.out.println("输入员工姓名");
        String name=input.next();
        System.out.println("输入员工工资");
        int sal=input.nextInt();
        System.out.println("输入员工部门");
        String dept=input.next();
        employee.setId(id);
        employee.setName(name);
        employee.setSal(sal);
        employee.setDept(dept);
        for (int i = 0; i <list1.size(); i++) {
            System.out.println(list1.get(i).getId()+" "+list1.get(i).getName()+" "+list1.get(i).getSal()+" "+list1.get(i).getDept());
        }
    }

    @Override
    public void show() {
        Employee e = null;
        for (int i = 0; i < list1.size(); i++) {
          e=list1.get(i);
            System.out.println(e.getId()+" "+e.getName()+" "+e.getSal()+" "+e.getDept());
        }
    }

    @Override
    public Employee getQueryById(int id) {
        Employee employee=null;
        for (int i = 0; i <list1.size(); i++) {
            if (id == list1.get(i).getId()) {
                employee=list1.get(i);
            }
        }
        return employee;
    }

    public  void  menu(){
        System.out.println("----------------请选择功能------------------");
        System.out.println("--------1.输入信息--2.查看所有员工信息-------");
        System.out.println("3.删除员工信息--4.修改员工信息--5.根据id查找员工信息");
    }

    public static void main(String[] args) {
        System.out.println("------------欢迎进入员工管理系统------------");
        EmployeeDAOImpl employeeDAO=new EmployeeDAOImpl();
        Scanner input= new Scanner(System.in);
        boolean isFlag;
        do {
            employeeDAO.menu();
            int choose=input.nextInt();
            switch (choose){
                case 1:                    //录入员工信息
                    String in;
                    do {
                            System.out.println("输入员工id");
                            int id=input.nextInt();
                            System.out.println("输入员工姓名");
                            String name=input.next();
                            System.out.println("输入员工工资");
                            int sal=input.nextInt();
                            System.out.println("输入员工部门");
                            String dept=input.next();
                            Employee e=new Employee(id,name,sal,dept);
                            employeeDAO.add(e);
                        do {
                            System.out.println("是否继续添加(y/n)");
                            in=input.next();
                            if (in.equals("y")) {
                                isFlag=true;
                                break ;
                            }else if(in.equals("n")){
                                isFlag=false;
                                break ;
                            }else {
                                System.out.println("输入有误!请重新输入！");
                                isFlag=true;
                            }
                        }while (isFlag);
                    }while (isFlag);
                    System.out.println("录入信息完毕！");
                    isFlag=true;
                    break;
                case 2:
                    employeeDAO.show();     //查看所有员工信息
                    isFlag=true;
                    break;
                case 3:
                    do {
                        System.out.println("请输入要删除员工信息的id");  // 根据id删除员工信息
                        int id3=input.nextInt();
                        Employee employee2=employeeDAO.getQueryById(id3);   // 先根据id查询员工信息
                        if (employee2 != null) {
                            employeeDAO.delete(employee2);   //在根据id查出来的信息进行删除
                            break;
                        }else {
                            System.out.println("id输入有误！请重新输入！");
                            isFlag=true;
                        }
                    }while (isFlag);
                    System.out.println("删除信息完毕！");
                    isFlag=true;
                    break;
                case 4:
                    do {
                        System.out.println("请输入要修改员工信息的id");  // 根据id修改员工信息
                        int id2=input.nextInt();
                        Employee employee1=employeeDAO.getQueryById(id2);   // 先根据id查询员工信息
                        if (employee1 != null) {
                            employeeDAO.updata(employee1);   //在根据id查出来的信息进行修改
                            break;
                        }else {
                            System.out.println("输入有误！请重新输入！");
                            isFlag=true;
                        }
                    }while (isFlag);
                    System.out.println("修改信息完毕！");
                    isFlag=true;
                    break;
                case 5:
                    do {
                        System.out.println("请输入要查询员工信息的id");  // 根据id查询员工信息
                        int id1=input.nextInt();
                        Employee employee=employeeDAO.getQueryById(id1);
                        if (id1 == employee.getId()) {
                            System.out.println(employee.getId()+" "+employee.getName()+" "+employee.getSal()+" "+employee.getDept());
                            isFlag=false;
                        }else {
                            System.out.println("id输入有误！请重新输入！");
                            isFlag=true;
                        }
                    }while (isFlag);
                    isFlag=true;
                    break;
                default:
                    System.out.println("输入有误请重新输入！");
                    isFlag=true;
            }
        }while (isFlag);
    }
}
